﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Panny.Data;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace Easydp.Models
{
    public class TravlerManager
    {
        public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        /// <summary>
        /// 返回TT_Travler表
        /// </summary>
        public Entities.TT_Travler GetAllTravler()
        {
            Entities.TT_Travler obj = new Easydp.Models.Entities.TT_Travler();
            using (SqlText selection = new SqlText("SELECT * FROM TT_Travler "))
            {

                while (selection.Read())
                {
                    obj.UserID = int.Parse(selection["UserID"].ToString());
                    obj.UserName = selection["UserName"].ToString();
                    obj.Email = selection["Email"].ToString();
                    obj.TelPhone = selection["TelPhone"].ToString();
                    obj.Address = selection["Address"].ToString();
                    if (selection["IsChecked"].ToString() != "")
                    {
                        if ((selection["IsChecked"].ToString() == "1") || (selection["IsChecked"].ToString().ToLower() == "true"))
                        {
                            obj.IsChecked = true;
                        }
                        else
                        {
                            obj.IsChecked = false;
                        }
                    }
                }
            }
            return obj;
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        public List<Entities.TT_Travler> GetListTravler(int UserID)
        {
            List<Entities.TT_Travler> list = new List<Easydp.Models.Entities.TT_Travler>();
            using (SqlText selectlist = new SqlText("select * from TT_Travler where UserID=@UserID"))
            {
                selectlist.AddParameter("@UserID", UserID);
                while (selectlist.Read())
                {
                    Entities.TT_Travler obj = new Easydp.Models.Entities.TT_Travler();
                    obj.UserID = int.Parse(selectlist["UserID"].ToString());
                    obj.UserName = selectlist["UserName"].ToString();
                    obj.Email = selectlist["Email"].ToString();
                    obj.TelPhone = selectlist["TelPhone"].ToString();
                    obj.Address = selectlist["Address"].ToString();
                    if (selectlist["IsChecked"].ToString() != "")
                    {
                        if ((selectlist["IsChecked"].ToString() == "1") || (selectlist["IsChecked"].ToString().ToLower() == "true"))
                        {
                            obj.IsChecked = true;
                        }
                        else
                        {
                            obj.IsChecked = false;
                        }
                    }
                    list.Add(obj);
                }
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM TT_Travler ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            
            using (SqlConnection sqlcon=new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    sqlcon.Open();
                    SqlDataAdapter command = new SqlDataAdapter(strSql.ToString(), connectionString);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }			
                finally
                {
                   sqlcon.Close();
                }	
                 return ds;
            }
           
        }

        /// <summary>
        /// 得到模型
        /// </summary>
        public Entities.TT_Travler GetModel(int UserID)
        {
            Entities.TT_Travler model = new Easydp.Models.Entities.TT_Travler();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserID,UserName,Email,TelPhone,Address,IsChecked from TT_Travler ");
            strSql.Append(" where UserID=@UserID ");
            using (SqlText selection = new SqlText(strSql.ToString()))
            {
                selection.AddParameter("@UserID", UserID);
                while (selection.Read())
                {
                        model.UserID = int.Parse(selection["UserID"].ToString());
                        model.UserName = selection["UserName"].ToString();
                        model.Email = selection["Email"].ToString();
                        model.TelPhone = selection["TelPhone"].ToString();
                        model.Address = selection["Address"].ToString();
                   if (selection["IsChecked"].ToString() != "")
                    {
                        if ((selection["IsChecked"].ToString() == "1") || (selection["IsChecked"].ToString().ToLower() == "true"))
                        {
                            model.IsChecked = true;
                        }
                        else
                        {
                            model.IsChecked = false;
                        }
                    }
                    
                }
            }
            return model;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Entities.TT_Travler model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TT_Travler set ");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Email=@Email,");
            strSql.Append("TelPhone=@TelPhone,");
            strSql.Append("Address=@Address,");
            strSql.Append("IsChecked=@IsChecked");
            strSql.Append(" where UserID=@UserID ");
            using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@UserName", model.UserName);
                updation.AddParameter("@Email", model.Email);
                updation.AddParameter("@TelPhone", model.TelPhone);
                updation.AddParameter("@Address", model.Address);
                updation.AddParameter("@IsChecked", model.IsChecked);
               
                return updation.ExecuteNonQuery();

            }

        }

    }
}
